package models

import (
	"time"

	"github.com/jinzhu/gorm"
)

type MindShare struct {
	Model
	Id          int          `json:"-"`
	Acid        int          `json:"acid"`
	Pid         string       `json:"pid"`
	Url         string       `json:"url"`
	Expires     int64        `json:"expires"`
	Pass        string       `json:"pass"`
	Checktype   int          `json:"checktype"`
	Shareid     string       `json:"shareid"`
	Types       string       `json:"t"`
	IsEdit      int          `json:"is_edit"`
	Fileinfo    MindShareCol `json:"fileinfo"`
	Version     int          `json:"version"`
	Createtime  int64        `json:"createtime"`
	Modifietime int64        `json:"modifietime"`
}
type MindShareCol struct {
	Filename string `json:"filename"`
	Image    string `json:"image"`
}
type mindshares struct {
	Shareid    string `json:"shareid"`
	Acid       int    `json:"acid"`
	Pid        string `json:"pid"`
	Url        string `json:"url"`
	Filename   string `json:"filename"`
	Image      string `json:"image"`
	Pass       string `json:"pass"`
	Checktype  string `json:"checktype"`
	Createtime int64  `json:"createtime"`
	Version    string `json:"version"`
	Types      string `json:"types"`
}

func (tag *MindShare) BeforeCreate(scope *gorm.Scope) error {
	scope.SetColumn("createtime", time.Now().Unix())

	return nil
}

func (tag *MindShare) BeforeUpdate(scope *gorm.Scope) error {
	scope.SetColumn("modifietime", time.Now().Unix())
	return nil
}

func GetMindShare(id string) (minds MindShare) {
	db.Where("Shareid = ?", id).First(&minds)
	return
}

func GetMindShares(PageNum int, PageSize int, userid int) (minds []*mindshares) {
	db.Raw("SELECT shares.pid, shares.shareid, shares.version, shares.pass, shares.checktype, shares.createtime,h.types, h.filename, h.image, shares.id, shares.acid, shares.url FROM md_mind_share AS shares  INNER JOIN(SELECT map.mindid,map.filename, map.image,\"m\" AS types from md_mindmap AS map WHERE map.state=0 AND map.sharestate=1 AND map.acid=? UNION SELECT map.mindid,map.filename, map.image,\"f\" AS types from md_mind_flow AS map WHERE map.state=0  AND map.sharestate=1 AND map.acid=?) as h ON h.mindid=shares.pid  WHERE shares.version = 1 ORDER BY shares.modifietime DESC", userid, userid).Scan(&minds)
	return minds
}

func GetMindSharesV5(PageNum int, PageSize int, userid int) (minds []*mindshares) {
	db.Raw("SELECT shares.pid, shares.shareid, shares.version, shares.pass, shares.checktype, shares.createtime,h.types, h.filename, h.image, shares.id, shares.acid, shares.url FROM md_mind_share AS shares INNER JOIN(SELECT map.mindid,map.filename, map.image,\"m\" AS types from md_mindmap AS map WHERE map.state=0 AND map.sharestate=1 AND map.acid=? UNION SELECT map.mindid,map.filename, map.image,\"f\" AS types from md_mind_flow AS map WHERE map.state=0 AND map.sharestate=1 AND map.acid=?) as h ON h.mindid=shares.pid ORDER BY shares.modifietime DESC", userid, userid).Scan(&minds)
	return minds
}

func AddMindShare(data map[string]interface{}) bool {
	db.Create(&MindShare{
		Acid:        data["acid"].(int),
		Pid:         data["pid"].(string),
		Url:         data["url"].(string),
		Expires:     data["expires"].(int64),
		Pass:        data["pass"].(string),
		Checktype:   data["checktype"].(int),
		Shareid:     data["shareid"].(string),
		Types:       data["types"].(string),
		Version:     data["version"].(int),
		Createtime:  data["createtime"].(int64),
		Modifietime: data["modifietime"].(int64),
	})
	return true
}

func EditMindShare(id string, maps interface{}) bool {
	db.Model(&MindShare{}).Where("shareid = ?", id).Update(maps)
	return true
}

func DeleteMindShare(id string, maps interface{}) bool {
	db.Where("shareid = ?", id).Delete(&MindShare{})
	return true
}

func ExistMindShareByID(id string) bool {
	var minds MindShare
	db.Where("shareid = ?", id).First(&minds)
	if minds.ID > 0 {
		return true
	}
	return false
}
func ExistMindShareVerify(id string, pass string) bool {
	var minds MindShare
	db.Where("shareid = ? and pass = ? ", id, pass).First(&minds)
	if minds.ID > 0 {
		return true
	}
	return false
}
func ExistMindShareByMindID(mindid string) bool {
	var minds MindShare
	db.Where("pid = ?", mindid).First(&minds)
	if minds.ID > 0 {
		return true
	}
	return false
}

func GetMindShareID(mindid string) (minds MindShare) {
	db.Where("pid = ?", mindid).First(&minds)
	return
}

func GetMindShareTotal(maps interface{}) (count int) {
	db.Model(&MindShare{}).Where(maps).Count(&count)
	return
}
